<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * 安全问题库
 * 当前模型，只对数据库做简单的处理,
 * 有些公共模型本可以封装到MY_DBModel里的，现在不做较大变动
 * @author hans
 */
class Ask_model extends CI_Model {

    /**
     * 数据库名rsh_ask
     * @var string
     */
    protected $_table = 'rsh_ask';

    /**
     * 主键ID
     */
    protected $_primaryKey = 'id';

    /**
     * 简单尾部组合
     */
    protected $_where = '';
    protected $_order = ' id desc ';
    protected $_limit;
    protected $_fields = '*';

    /**
     * 设置排序
     * @param string $order
     * @return \Ask_model
     */
    public function orderBy($order) {
        $this->_order = $order;
        return $this;
    }

    /**
     * 获取安全问题数据
     * @param array $cond   获取条件
     * @param array/string $feilds 获取列信息 默认获取所有字段
     */
    public function get($cond = array(), $pageIndex = 0, $perPage = 10, $feilds = ' * ') {
        $this->_whereSet($cond)->_limit($pageIndex, $perPage);
        $result = array();
        $result['list'] = $this->_whereSet($cond)->_limit($pageIndex, $perPage)->_getAll();
        $result['count'] = $this->_whereSet($cond)->_count();

        return $result;
    }

    /**
     * 获取当前制定ID的数据
     * @param type $id  问题ID
     * @return type
     */
    public function getOne($id) {
        $this->_whereSet(array('id' => $id, 'isDel' => 0));
        $sql = $this->_makeQuery();
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    /**
     * 保存数据  如果含有索引主键，则为编辑保存，否则为新增
     * @param array $data
     */
    public function save($data) {
        if (isset($data[$this->_primaryKey]) && $data[$this->_primaryKey] > 0) {
            $key = $data[$this->_primaryKey];
            unset($data[$this->_primaryKey]);
            $this->db->where($this->_primaryKey, $key);
            $this->db->update($this->_table, $data);
            return $this->db->affected_rows();

        } else {
            $this->db->insert($this->_table, $data);
            return $this->db->insert_id();
        }
    }

    /**
     * 删除数据
     * @param type $id
     * @return type
     */
    public function delete($id){
        $this->db->where($this->_primaryKey, $id);
        $this->db->update($this->_table, array('isDel' => 1));
        return $this->db->affected_rows();
    }

    /**
     * 获取数据列表
     * @return type
     */
    private function _getAll() {
        $sql = $this->_makeQuery();
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 获取查询记录条数
     * @return type
     */
    private function _count() {
        $sql = $this->_makeQuery('count');
        $query = $this->db->query($sql);
        return $query->row()->count;
    }

    /**
     * 创建条件语句
     * @param array $cond    条件数组
     * @param string $connect     链接方式
     */
    private function _whereSet($cond = array(), $connect = 'and') {
        //组织条件
        $condition = '';
        if (is_string($cond)) {
            $condition = $cond;
        } elseif (is_array($cond)) {
            foreach ($cond as $key => $value) {
                $and = $condition == '' ? '' : ' and ';
                if (is_numeric($key)) {
                    $condition .= $and . $value;
                } else {
                    $condition .= $and . $key . '=' . $value;
                }
            }
        }

        $this->_where = $condition;
        return $this;
    }

    /**
     * 设置分页信息
     * @param int $pageIndex   当前第几页
     * @param type $perPage     每页多少条
     * @return \Ask_model
     */
    private function _limit($start = 0, $perPage = 10) {
        //分页;
        $this->_limit = " limit $start, $perPage";

        return $this;
    }

    /**
     * 组织sql
     * @param string $type
     * @return string
     */
    private function _makeQuery($type = 'select') {
        $sql = null;
        switch ($type) {
            case 'count':
                $sql = "select count(*) as count from $this->_table ";
                break;
            case 'select':
                $sql = "SELECT $this->_fields FROM $this->_table ";
                break;
            case 'delete':
                $sql = "DELETE FROM $this->_table ";
                break;
            case 'update':
                $sql = "UPDATE $this->_table SET $this->_update ";
                break;
        }

        $this->_where and $sql .= " where $this->_where ";

        //非查询语句不需要这些
        if ($type == 'select') {
            $this->_order and $sql .= " ORDER BY $this->_order ";
            $this->_limit and $sql .= $this->_limit;
        }

        $sql .= ';';

        $this->_fields = '*';
        $this->_where = null;
        $this->_order = null;
        $this->_limit = null;
        $this->_update = null;
        return $sql;
    }
}
